/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.maternity.classes.dao;

import br.com.maternity.classes.entity.Paciente;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Gattini
 */
public class PacienteDAO extends GenericDAO<Paciente> {

    private static Paciente instance = new Paciente();

    public PacienteDAO() {
        super(instance);
    }

    @Override
    public List<GenericDAOField> listFields() {
        List l = new ArrayList<GenericDAOField>();

        l.add(new GenericDAOField(
                "Prontuario", "setProntuario", "getProntuario", Integer.class, true, GenericDAOFieldPrimaryKey.IDENTITY));
        l.add(new GenericDAOField(
                "Nome", "setNome", "getNome", String.class));
        l.add(new GenericDAOField(
                "Telefone", "setTelefone", "getTelefone", String.class));
        l.add(new GenericDAOField(
                "DataNascimento", "setDataNascimento", "getDataNascimento", Date.class));
        l.add(new GenericDAOField(
                "CPF", "setCpf", "getCpf", String.class));
        l.add(new GenericDAOField(
                "RG", "setRg", "getRg", String.class));
        l.add(new GenericDAOField(
                "Logradouro", "setLogradouro", "getLogradouro", String.class));
        l.add(new GenericDAOField(
                "Numero", "setNumero", "getNumero", String.class));
        l.add(new GenericDAOField(
                "Complemento", "setComplemento", "getComplemento", String.class));
        l.add(new GenericDAOField(
                "CEP", "setCep", "getCep", String.class));
        l.add(new GenericDAOField(
                "Bairro", "setBairro", "getBairro", String.class));
        l.add(new GenericDAOField(
                "Cidade", "setCidade", "getCidade", String.class));
        l.add(new GenericDAOField(
                "UF", "setUf", "getUf", String.class));
        l.add(new GenericDAOField(
                "Cor", "setCor", "getCor", Integer.class));
        l.add(new GenericDAOField(
                "CartaoSUS", "setCartaoSUS", "getCartaoSUS", String.class));
        l.add(new GenericDAOField(
                "NomePai", "setNomePai", "getNomePai", String.class));
        l.add(new GenericDAOField(
                "NomeMae", "setNomeMae", "getNomeMae", String.class));
        l.add(new GenericDAOField(
                "Sexo", "setSexo", "getSexo", Integer.class));

        return l;
    }

    @Override
    public String tableName() {
        return "Paciente";
    }

    public Paciente getByProntuario(Integer prontuario) {
        try {
            String query = "SELECT * FROM " + tableName() + " WHERE Prontuario = ?";
            PreparedStatement ps = super.getPreparedStatement(query);
            ps.setInt(1, prontuario);
            return select(ps);
        } catch (Exception ex) {
            return null;
        }
    }

    public Paciente getByCpf(String cpf) {
        try {
            String query = "SELECT * FROM " + tableName() + " WHERE CPF = ?";
            PreparedStatement ps = super.getPreparedStatement(query);
            ps.setString(1, cpf);
            return select(ps);
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Paciente> selectByRgOrNome(String rg, String nome) {
        try {
            String query = "SELECT * FROM " + tableName()
                    + " WHERE (? = '' OR RG = ?) AND (Nome Like ?) ORDER BY Nome";
            PreparedStatement ps = super.getPreparedStatement(query);
            ps.setString(1, rg);
            ps.setString(2, rg);
            ps.setString(3, nome);
            return selectList(ps);
        } catch (Exception ex) {
            return new ArrayList<Paciente>();
        }
    }
}
